package com.shou.trigger.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.DeleteDTO;
import com.shou.api.model.dto.PageQueryDTO;
import com.shou.api.model.dto.ReservationDTO;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.reserve.model.ReservationEntity;
import com.shou.domain.reserve.service.IReservationService;
import com.shou.trigger.http.mapstruct.ReservationDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName:ReservationController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/12 21:10
 * @Version 1.0
 */
@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/reservation")
public class ReservationController extends AbstructController{
    @Resource
    private IReservationService reservationService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private ReservationDTOConvert reservationDTOConvert;

    /**
     * 分页查询
     */
    @GetMapping("/reservation-info")
    public ResultData<IPage<ReservationDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<ReservationEntity> queueMessageList = reservationService.pageQuery(pageQueryEntity);
        return ResultData.success(reservationDTOConvert.convertReservationEntityPageToDTO(queueMessageList));
    }

    /**
     * 新增预定信息
     */
    @PostMapping("/reservation-info")
    public ResultData<String> insertReservation(@RequestBody ReservationDTO reservationDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        ReservationEntity reservationEntity = reservationDTOConvert.convertReservationDTOToEntity(reservationDTO);
        // 保存(新增)规则
        reservationService.insertReservation(reservationEntity);
        return ResultData.success("insertReservation success");
    }

    /**
     * 删除单/多个预定信息
     */
    @DeleteMapping("/reservation-info")
    public ResultData<String> deleteReservationByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除预定信息
        reservationService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteReservationByIds success");
    }

    /**
     * 清空所有预定信息
     */
    @DeleteMapping("/all-reservation-info")
    public ResultData<String> deleteAll() {
        // 清空预定信息表
        reservationService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据预定ID更新预定信息
     */
    @PutMapping("/reservation-info")
    public ResultData<String> updateReservationById(@RequestBody @Validated ReservationDTO rule) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        ReservationEntity reservationEntity = reservationDTOConvert.convertReservationDTOToEntity(rule);
        // 根据ID更新预定信息
        reservationService.updateReservationById(reservationEntity);
        return ResultData.success("updateReservationById success");
    }
}
